﻿	
    /**
     *  Simple class for collect basic propramaticly animation action.
     *  For use this class add Tweener library to your project.
     * 
     *  Show text or graphics has used <code>init</code> method.
     *  Hide text has used <code>remove</code>
     *
     *  Movie clip which use this class must define functions
     *  <code>
     *  addClip(movieClip, order, index);
     *  removeClip(movieClip, order, index);
     * </code>
     */
	class org.kolonitsky.alexey.banner.texteffect.BaseTextEffectAS2 extends MovieClip
	{
		
		public var order:Array;
		
		/**
		 * Constructor
		 */
		public function BaseTextEffectAS2()
		{
			super();
			order = [];
		}
		
		
		/**
		 *  init function collect all moveclip child, convert them to 
         *  MovieClip objects and run <code>addClip</code> function 
         *  for each clip.
         * 
         *  @param orderFunc passed for change default text order. It can 
         *  be any function with one array parameter for modify passed array.
         *  Default function randomOrder, reverseOrder
		 */
		public function init(orderFunc:Function):Void
		{
			// fill array
			var n:Number = 0;
			for (var prop in this) 
            {
			    if (this[prop] instanceof MovieClip) 
                    n++;
                else
                    trace("Unexpected object on stage: " + this[prop]);
            }
			
			trace("init() n= " + n);
			for (var i:Number = 0; i < n; i++)
				order[i] = i;
			
			if (orderFunc != null)
				orderFunc(order);
			
			i = 0.0;
			for (var prop in this) 
			{
				var mc:MovieClip;
				
				if (this[prop] instanceof MovieClip)
				{
					mc = this[prop];
					this["addClip"](mc, order[i], i);
					i++;
				}
				else 
				{
					removeMovieClip(child);
				}
			}
			
		}
		
		/**
		 *  Hide all elements.
		 */
		public function remove():Void
		{
			var n:Number = order.length;
			var i:Number = 0.0;
			for (var prop in this) 
			{
				if (this[prop] instanceof MovieClip)
				{
					this["removeClip"](this[prop], order[i], i);
					i++
				}
			}
		}
		
		
		/**
		 *
		 */
		public function randomOrder(ar:Array):Void
		{
			var n:Number = ar.length;
			for (var i:Number=0; i < n; i++) 
			{
				var ind: Number = Math.random() * n;
				var xch: Number = ar[ind];
				ar[ind] = ar[i];
				ar[i] = xch;
			}
		}
		
		
		/**
		 *
		 */
		public function reversOrder(ar:Array):Void 
		{
			ar.reverse();
		}

	}
